package tbalert.web.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import tbalert.hibernate3.Finder;
import tbalert.hibernate3.HibernateBaseDao;
import tbalert.page.Pagination;
import tbalert.web.dao.DwdmDao;
import tbalert.web.entity.Dwdm;
import tbalert.web.entity.Role;

public class DwdmDaoImpl extends HibernateBaseDao<Dwdm, String> implements DwdmDao {

	public Dwdm deleteById(String id) {
		Dwdm entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	public Pagination find(Finder finder, int pageNo, int pageSize) {
		return super.find(finder, pageNo, pageSize);
	}

	public Dwdm findById(String id) {
		Dwdm entity = get(id);
		return entity;
	}

	@SuppressWarnings("unchecked")
	public List<Dwdm> findBySjdw(String sjdwId) {
		if (StringUtils.equals(sjdwId, "00")) {
			sjdwId = "";
		}
		String hql = "from Dwdm where dwid like '" + sjdwId + "%' order by dwid";
		return find(hql);
	}

	public Dwdm getByDlmc(String dlmc) {
		return findUniqueByProperty("dlmc", dlmc);
	}

	public Dwdm getByRole(Role role) {
		return findUniqueByProperty("role", role);
	}

	@Override
	public String getMaxDwid(String sjdwId) {
		String sql = "select max(dwid) from sys_dwdm where dwid like '" + sjdwId + "__'";
		return (String) getSession().createSQLQuery(sql).uniqueResult();
	}

	public Dwdm save(Dwdm bean) {
		getSession().save(bean);
		return bean;
	}

	@Override
	protected Class<Dwdm> getEntityClass() {
		return Dwdm.class;
	}
}
